package com.hk.service.impl;

import com.hk.dao.IAdminDao;
import com.hk.dao.impl.AdminDaoImpl;
import com.hk.model.Admin;
import com.hk.service.IAdminService;
import com.hk.utils.DateUtils;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/5 18:24
 * @Statement: 管理员账户业务逻辑层
 */
public class AdminServiceImpl implements IAdminService {

    IAdminDao iAdminDao = new AdminDaoImpl();

    @Override
    public Admin findByEmailAndPassword(Admin admin) {
        ResultSet resultSet = iAdminDao.findByEmailAndPassword(admin);
        Admin admin1 = transverter(resultSet);
//        关闭资源
        JdbcMysql.jdbcClose();
        return admin1;
    }

    @Override
    public int findByEmail(String email) {
        ResultSet resultSet = iAdminDao.findByEmail(email);
        Admin admin = transverter(resultSet);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (!admin.equals(null)) {
            System.out.println("邮箱已存在，请更换邮箱！");
            return 1;
        }
        return 0;
    }

    /**
     * 将传递的数据进行转换成对象
     * @param resultSet
     * @return
     */
    public Admin transverter(ResultSet resultSet ) {
        Admin admin = new Admin();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                String password = resultSet.getString("password");
                int sex = resultSet.getInt("sex");
                String birthday = resultSet.getString("birthday");
                String phone = resultSet.getString("phone");
                String site = resultSet.getString("site");
                Timestamp gmt_create = resultSet.getTimestamp("gmt_create");
                Timestamp gmt_modified = resultSet.getTimestamp("gmt_modified");
//                封装对象
                admin.setId(id);
                admin.setName(name);
                admin.setEmail(email);
                admin.setPassword(password);
                admin.setSex(sex);
                admin.setBirthday(birthday);
                admin.setPhone(phone);
                admin.setSite(site);
                String gmtCreate = DateUtils.dateTimeString(gmt_create);
                String gmtModified = DateUtils.dateTimeString(gmt_modified);
                admin.setGmtCreate(gmtCreate);
                admin.setGmtModified(gmtModified);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return admin;

    }


    @Override
    public List<Admin> findAll(Integer page, Integer limit, Admin t) {
        return null;
    }

    @Override
    public int count(Integer page, Integer limit, Admin t) {
        return 0;
    }

    @Override
    public Admin findById(Integer id) {
        ResultSet resultSet = iAdminDao.findById(id);
        Admin admin = transverter(resultSet);
        //        关闭资源
        JdbcMysql.jdbcClose();
        return admin;
    }

    @Override
    public int save(Admin admin) {
        return 0;
    }

    @Override
    public int update(Admin admin) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        admin.setGmtModified(dateTimeString);
        int update = iAdminDao.update(admin);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (update == 1) {
            System.out.println("当前管理员信息修改成功");
        }
        return update;
    }

    @Override
    public int updatePassWord(Admin admin) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        admin.setGmtModified(dateTimeString);
        int updatePassWord = iAdminDao.updatePassWord(admin);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (updatePassWord == 1) {
            System.out.println("当前管理员密码修改成功");
        }
        return updatePassWord;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }
}
